import { Schema, model } from "mongoose";
import type Typings from "../../typings";

import { createDb } from "../../mongoose/mongoose";
const db = createDb("common", "project");

// 定义 Schema 模型
const ProjectSchema = new Schema<Typings.Project.props>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  parentCode: {
    type: String,
    default: "",
  },

  code: {
    type: String,
    required: true,
  },

  name: {
    type: String,
    required: true,
  },

  tag: {
    type: String,
    default: "primary",
  },

  children: {
    type: Array,
    default: [],
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
ProjectSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

ProjectSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

const ProjectModel = db.model<Typings.Project.props>(
  "Project",
  ProjectSchema,
  "projects"
);

export default ProjectModel;
